<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="keyword" content="ONE,GO物,网上购物,交易市场,网上交易">
    <meta name="description" content="ONE-GO物，平价线上交易平台，提供各类商品交易，让大家都可以享受购物的乐趣和真正品质购物">
    <title>ONE - GO物 ! 注册</title>
    <link rel="shortcut icon" href="/static/images/logo_i.png">
    <!--引入layui css样式-->
    <link rel="stylesheet" href="/static/layui-v2.6.7/layui/css/layui.css">
    <!--引入layui js脚本-->
    <script src="/static/layui-v2.6.7/layui/layui.js"></script>
    <link rel="stylesheet" href="/static/css/register.css">
</head>
<body class="layui-bg-red">
<div class="content layui-container layui-anim layui-anim-scale">
    <div class="logo-box">
        <a href="/static/index.html">
            <img src="/static/images/logo_w.png" alt="ONE-GO物" class="logo">
            <h2>ONE-GO物</h2>
            <p>2021 - 现在 yige@yige.com 版权所有</p>
        </a>
    </div>
    <div class="form-box">
        <h3 class="title">欢迎注册</h3>
        <form class="layui-form" action="" lay-filter="example">
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱/账号</label>
                <div class="layui-input-block">
                    <input type="text" name="email" required lay-verify="required|email" autocomplete="off"
                           placeholder="请输入邮箱/账号"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="password" required lay-verify="required|pass" placeholder="请输入密码"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">确认密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="password" required lay-verify="required|pass|re_pass"
                           placeholder="请确认密码"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱验证码</label>
                <div class="layui-input-inline">
                    <input type="text" name="verify" required lay-verify="required|verify" placeholder="请输入邮箱验证码"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <button type="button" class="layui-btn layui-btn-danger" id="send_email">发送验证码</button>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="register">立即注册</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
        <div class="go-login"><a href="/static/page/user/login.html">已有账号，前往登陆</a></div>
    </div>
</div>
<script type="text/javascript">
    layui.use(['form', 'layedit', 'laydate', 'jquery'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , $ = layui.$;

        //自定义验证规则
        form.verify({
            pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ],
            re_pass: function (value, item) {
                if (value != $('input[name="password"]').val()) {
                    return '两次密码不一致'
                }
            },
            verify: [
                /^[\d]{6}$/
                , '验证码必须是6位'
            ],
        });

        //监听提交
        form.on('submit(register)', function (data) {
            $.post("/user/register/", data.field, function (data) {
                let code = data.code
                if (code == 4001 || code == 4003) {
                    layer.msg(data.msg, {icon: 5})
                    return false
                }
                if (code == 200) {
                    layer.msg('邮箱/账号注册成功', {
                        time: 5000, //20s后自动关闭
                        btn: ['立即登陆', '继续注册'],
                        yes: function (index, layero) {
                            //按钮【按钮一】的回调
                            // console.log('去登陆')
                            location.href = '/static/page/user/login.html'
                        }
                    });
                }
                return false
            })
            return false
        });

        // 发送邮件验证码
        let flag = true // 标记
        let time_ex = 59
        let time_count = time_ex

        // 获取localStorage中指定key的value值
        let local_time_count = localStorage.getItem('time_count')

        function send_email_style() {
            $('#send_email').addClass('layui-btn-disabled')
            flag = false
            let time_fun = function () {
                $('#send_email').text(time_count + '秒后请重试')
                if (time_count == 0) {
                    clearInterval(time_int)
                    flag = true
                    time_count = time_ex
                    $('#send_email').removeClass('layui-btn-disabled')
                    $('#send_email').text('发送验证码')

                    // 清除localStorage指定的key和value值
                    localStorage.removeItem('time_count')
                    return false
                }
                time_count--
                // localStorage的赋值
                localStorage.setItem('time_count', time_count)
            }
            time_fun()
            let time_int = setInterval(time_fun, 1000)
        }

        if (local_time_count) {
            // 禁用
            time_count = local_time_count
            send_email_style()
        }
        $('#send_email').click(function () {
            email = $('input[name="email"]').val()
            if (!email) {
                layer.msg('请输入邮箱', {icon: 5})
                return false
            }

            let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
            if (!reg.test(email)) {
                layer.msg('邮箱格式不正确', {icon: 5})
                return false
            }

            if (!flag) {
                layer.msg('请稍后重试', {icon: 5})
                return false
            }

            $.post("/user/email/verification/", {email: email}, function (data) {
                if (data.code == 4002) {
                    layer.msg('请稍后重试', {icon: 5})
                    return false
                }
            })

            send_email_style()
        })
    });
</script>
</body>
</html>